package com.ycl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ycl.common.BaseThread;
import com.ycl.common.Result;
import com.ycl.entity.ShoppingCart;
import com.ycl.service.ShoppingCartService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class shoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车-shoppingCart：{}",shoppingCart);
        // 设置用户ID，指定当前是那个用户的数据
        Long userId = BaseThread.getCurrentId();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        // 如果添加的是菜品
        if ( shoppingCart.getDishId() != null){
            Long dishId = shoppingCart.getDishId();
            String dishFlavor = shoppingCart.getDishFlavor();
            queryWrapper
                    .eq(ShoppingCart::getDishId,dishId);
        }
        // 反之，如果添加的是套餐
        else {
            Long setmealID = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealID);
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // 第一次添加
        if (one == null){
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            one = shoppingCart;
            shoppingCartService.save(one);
        }
        // 反之，第多次添加
        else {
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }
        // 添加到购物车
        return Result.success(one);
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车");
        Long userID = BaseThread.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(userID != null , ShoppingCart::getUserId , userID)
                .orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 减少菜品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车-sub：{}",shoppingCart);
        // 设置用户ID，指定当前是那个用户的数据
        Long userId = BaseThread.getCurrentId();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        // 如果添加的是菜品
        if ( shoppingCart.getDishId() != null){
            Long dishId = shoppingCart.getDishId();
            String dishFlavor = shoppingCart.getDishFlavor();
            queryWrapper
                    .eq(ShoppingCart::getDishId,dishId);
        }
        // 反之，如果添加的是套餐
        else {
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        if (number.equals(1)){
            shoppingCartService.removeById(one);
        }
        else {
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }
        return Result.success(one);
    }

    /**
     * 清除购物车
     * @return
     */
    @DeleteMapping("clean")
    public Result<String> clean(){
        Long userId = BaseThread.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return Result.success("清除成功");
    }
}
